package com.jia.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jia.pojo.MyUser;
import com.jia.pojo.MyUserRole;
import com.jia.pojo.Role;
import com.jia.service.MyUserRoleService;
import com.jia.service.MyUserService;
import com.jia.service.RoleService;
import com.jia.vo.MyUserList;
import com.jia.vo.MyUserSelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 贾宇光
 * @version 1.0
 * @date 2020/9/17 9:42
 */
@Controller
@RequestMapping("/myuser")
public class MyUserController {
    @Autowired
    MyUserService myUserService;
    @Autowired
    RoleService roleService;
    @Autowired
    MyUserRoleService myUserRoleService;

    @GetMapping("/select")
    public String myUserSelect(Model model, HttpSession session) {
        MyUser myUserInfo = (MyUser) session.getAttribute("myUserInfo");
        Role role = roleService.queryRoleByUid(myUserInfo.getUid());
        List<MyUserList> myUserList = myUserService.queryAllByRid(role.getRid());
        Map<Integer, MyUserSelect> myUserMap = new HashMap<>();
        for (MyUserList list : myUserList) {
            if (myUserMap.containsKey(list.getUid())) {
                MyUserSelect myUserSelect = myUserMap.get(list.getUid());
                myUserSelect.getAname().add(list.getAname());
                myUserSelect.getAdescription().add(list.getAdescription());
            } else {
                MyUserSelect myUserSelect = new MyUserSelect();
                myUserSelect.setUsername(list.getUsername());
                myUserSelect.setPassword(list.getPassword());
                myUserSelect.setRid(list.getRid());
                myUserSelect.setRname(list.getRname());
                myUserSelect.setRdescription(list.getRdescription());
                myUserSelect.setAid(list.getAid());
                ArrayList<String> anames = new ArrayList<>();
                anames.add(list.getAname());
                myUserSelect.setAname(anames);
                ArrayList<String> adescriptions = new ArrayList<>();
                adescriptions.add(list.getAdescription());
                myUserSelect.setAdescription(adescriptions);
                myUserMap.put(list.getUid(), myUserSelect);
            }
        }
        model.addAttribute("myUserMap", myUserMap);
        return "pages/myuser/selectMyUser";
    }

    @GetMapping("/update/{uid}/{username}/{rid}")
    public String myUserUpdate(@PathVariable("uid") int uid,
                               @PathVariable("username") String username,
                               @PathVariable("rid") int rid,
                               Model model,
                               HttpSession session) {
        MyUser myUserInfo = (MyUser) session.getAttribute("myUserInfo");
        Role role = roleService.getOne(new QueryWrapper<Role>().eq("rid", myUserInfo.getUid()));
        List<Role> rolelist = roleService.list(new QueryWrapper<Role>().le("rid", role.getRid()));
        model.addAttribute("rolelist", rolelist);
        model.addAttribute("uid", uid);
        model.addAttribute("username", username);
        model.addAttribute("rid", rid);
        return "pages/myuser/updateMyUser";
    }

    @PostMapping("/updateData")
    public String myUserUpdateData(int uid, String newPassword, String roles, int rid) {
        if (newPassword.length() > 0) {
            MyUser myUser = new MyUser();
            myUser.setUid(uid);
            myUser.setPassword(BCrypt.hashpw(newPassword, BCrypt.gensalt()));
            myUserService.updateById(myUser);
        }

        MyUserRole myUserRole = new MyUserRole();
        myUserRole.setUid(uid);
        myUserRole.setRid(Integer.valueOf(roles));
        myUserRoleService.update(myUserRole,
                new QueryWrapper<MyUserRole>().eq("uid", uid).eq("rid", rid));
        return "index";
    }

    @GetMapping("/delete/{uid}")
    public String myUserDelete(@PathVariable("uid") int uid){
        myUserService.removeById(uid);
        return "index";
    }

    @GetMapping("/add")
    public String myUserAdd(Model model,HttpSession session){
        MyUser myUserInfo = (MyUser) session.getAttribute("myUserInfo");
        Role role = roleService.getOne(new QueryWrapper<Role>().eq("rid", myUserInfo.getUid()));
        List<Role> rolelist = roleService.list(new QueryWrapper<Role>().le("rid", role.getRid()));
        model.addAttribute("rolelist", rolelist);
        return "pages/myuser/addMyUser";
    }

    @PostMapping("/addData")
    public String myUseraddData(String username, String password, String roles) {
        MyUser myUser = new MyUser();
        myUser.setUsername(username);
        myUser.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
        myUserService.save(myUser);
        int uid = myUser.getUid();

        MyUserRole myUserRole = new MyUserRole();
        myUserRole.setUid(uid);
        myUserRole.setRid(Integer.valueOf(roles));
        myUserRoleService.save(myUserRole);
        return "index";
    }

}
